<?php

namespace Wap\Controller;

use Think\Model;

class FixController extends \Think\Controller
{
    //修复总贡献
    public function total_contribution()
    {
        $order_list = M('order')->where(['pay_status' => 1])->select();
        try {
            M('')->startTrans();
            $update = M('user')->where('uid > 0')->save([
                'total_contribution' => 0
            ]);
            if ($update === false) {
                throw new \Exception("total_contribution 清零失败");
            }
            $update = M('team_log')->where('uid > 0')->delete();
            if ($update === false) {
                throw new \Exception("team_log 清空失败");
            }
            foreach ($order_list as $order) {
                $update = M('user')->where(['uid' => $order['uid']])->save([
                    'total_contribution' => ['exp', "total_contribution+{$order['order_contribution']}"]
                ]);
                if ($update === false) {
                    throw new \Exception("total_contribution 累计失败");
                }
                $user_list = OE('user')->getRefererTree($order['uid']);
                foreach ($user_list as $user) {
                    OE('user')->addTeamContribution($user['uid'], $order);
                }
            }
            M('')->commit();
        } catch (\Exception $e) {
            M('')->rollback();
            throw new \Exception($e->getMessage());
        }
    }

    //修复总消费
    public function total_buy()
    {
        $order_list = M('order')->where(['pay_status' => 1])->select();
        try {
            M('')->startTrans();
            $update = M('user')->where('uid > 0')->save([
                'total_buy' => 0
            ]);
            if ($update === false) {
                throw new \Exception("total_buy 清零失败");
            }
            foreach ($order_list as $order) {
                $update = M('user')->where(['uid' => $order['uid']])->save([
                    'total_buy' => ['exp', "total_buy+{$order['pay_money']}"]
                ]);
                if ($update === false) {
                    throw new \Exception("pay_money 累计失败");
                }
            }
            M('')->commit();
        } catch (\Exception $e) {
            M('')->rollback();
            throw new \Exception($e->getMessage());
        }
    }

    public function test(){
        $user = session('user_auth');var_dump($user);exit();
            var_dump($_SERVER['REDIRECT_URL']);exit();
    }

    public function repairOrder(){
        echo "start#########".date("Ymd H:i:s")."#######";

        $model = new Model();
        $sql = "SELECT id,shopId,mid FROM `k_zs_order_pay` WHERE mid= 0;";
        $order_list = $model->query($sql);
        $i = 0;
        foreach($order_list as $k => $v){
            $seller = M('zs_seller')->where(['shopId' => $v['shopId']])->find();
            if(isset($seller['id'])){
                M('zs_order_pay')->where(['id' => $v['id']])->save(['mid' => $seller['mid']]);
                $i++;
                echo "{$i}\n";
                sleep(1);
            }
        }
        echo "更新了{$i}条\n";
        echo "end#########".date("Ymd H:i:s")."#######";exit();
    }

    public function repairGoods(){
        echo "start#########".date("Ymd H:i:s")."#######";
        $model  = new Model();
        $sql = "SELECT * FROM `k_zs_goods` WHERE mid IS NULL;";
        $goods_list = $model->query($sql);
        $i = 0;
        foreach($goods_list as $k => $v){
            $seller = M('zs_seller')->where(['shopId' => $v['shopId']])->find();
            if(isset($seller['id'])){
                M('zs_goods')->where(['id' => $v['id']])->save(['mid' => $seller['mid']]);
                $i++;
            }

        }
        echo "更新了{$i}条\n";
        echo "end#########".date("Ymd H:i:s")."#######";exit();
    }


    const LEVEL_VIP = [
        0 => [
            'level' => 0,
            'name' => '青铜',
            'money' => 3000,
            'days' => 15,
            'desc' => '入职15天为实习期 之后一个月内回款达到基础回款3000即可晋升为黑铁'
        ],
        1 => [
            'level' => 1,
            'name' => '黑铁',
            'money' => 6000,
            'days' => 30,
            'desc' => '入职30天为实习期 之后一个月内回款达到基础回款6000即可晋升为黑铁'
        ],
        2 => [
            'level' => 2,
            'name' => '白银',
            'money' => 9000,
            'days' => 45,
            'desc' => '入职45天为实习期 之后一个月内回款达到基础回款9000即可晋升为黑铁'
        ],
        3 => [
            'level' => 3,
            'name' => '黄金',
            'money' => 12000,
            'days' => 60,
            'desc' => '入职60天为实习期 之后一个月内回款达到基础回款12000即可晋升为黑铁'
        ],
        4 => [
            'level' => 4,
            'name' => '白金',
            'money' => 15000,
            'days' => 75,
            'desc' => '入职75天为实习期 之后一个月内回款达到基础回款15000即可晋升为黑铁'
        ],
        5 => [
            'level' => 5,
            'name' => '钻石',
            'money' => 18000,
            'days' => 90,
            'desc' => '入职90天为实习期 之后一个月内回款达到基础回款18000即可晋升为黑铁'
        ],
    ];

    public function userLevel(){
        var_dump(11231);
        $model = new Model();
        try{
            $model->startTrans();

            $group = M('auth_group')->field('*')->where(['is_zs' => 1])->select();
            $gid_arr = array_column($group, 'id');
            if(count($gid_arr) == 0)    throw new \Exception('升级失败-2');
            $group_access = M('auth_group_access')->where(['group_id' => ['in', $gid_arr]])->select();
            $uid_arr = array_column($group_access, 'uid');
            if(count($uid_arr) == 0)    throw new \Exception('升级失败-1');
            $where = ['uid' => ['in', $uid_arr]];
            $member_list =  M('member')->field('uid, nickname as name')->where($where)->select();

            $today = date("Y-m-d 23:59:59");
            foreach($member_list as $k =>$v){
                $level = 0;
                $yugu_money = 0;
                foreach(self::LEVEL_VIP as $k1 => $v1){
                    $zhi_day = date('Y-m-d H:i:s', strtotime("-{$v1['days']}days"));
                    $where = ['order_time'=>['between',[$zhi_day,$today]],'validCode'=>['in','16,17,18'], 'mid' => $v['uid']];
                    $yestoday_yugu = M('zs_order_pay')->where($where)->sum('estimateFee');
                    if($yestoday_yugu>= $v1['money']){
                        $level = $v1['level']+1;
                        $yugu_money =$v1['money'];
                    }
                }
                $flag = M('member')->where(['uid' => $v['uid']])->save(['level' =>$level, 'yugu_money' => $yugu_money]);
                if($flag === false) {
                    throw new \Exception('升级失败');
                }
            }

            $model->commit();

        } catch (\Exception $e) {
            $model->rollback();
            $msg = $e->getMessage();
            echo $msg;
        }
    }

    public function updataData(){
        $sql = "SELECT id, shopId FROM `k_zs_seller` WHERE mid is NULL;";
        $model = new Model();
        $seller_list = $model->query($sql);
        foreach($seller_list as $k => $v){
            if($v['shopId']){
                $order = M('zs_order_pay')->field('id, taobao_act_id, activity_id')->where(['shopId' => $v['shopId']])->order('id desc')->find();
                if($order['taobao_act_id']){

                    $activity =  M('zs_activity')->field('id, mid')->where(['id' =>$order['activity_id']])->find();
                    if($activity['id'] && $activity['mid']){
                        var_dump($k);
                        M('zs_seller')->where(['id' =>$v['id']])->save(['mid' => $activity['mid']]);
                    }
                }

            }
        }
    }
}
